<?php

namespace app\common\model;

use app\common\library\Auth;
use think\Db;
use think\Exception;
use think\Model;

/**
 * 菜单模型
 */
class Staff Extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 追加属性
    protected $append = [
    ];

    /**
     * 绑定员工
     * @param $userinfo
     * @param $username
     * @param $pwd
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function attestation($userinfo,$mobile,$pwd){
        $auth = new Auth();
        if($userinfo->group_id == 2){
            return [false,'您已绑定，请不要绑定'];
        }
        $member = self::where('mobile',$mobile)->find();
        if(empty($member)){
            return [false,'用户不存在'];
        }
        if($member->password != $auth->getEncryptPassword($pwd, $member->salt)){
            return [false,'密码错误'];
        }

        $member->user_id = $userinfo->id;
        Db::startTrans();
        try{
            $res = $member->save();
            if(!$res){
                Db::rollback();
                return [false,'绑定失败3'];
            }

            $userinfo->group_id = 2;
            $userSave = $userinfo->save();
            if(!$userSave){
                Db::rollback();
                return [false,'绑定失败4'];
            }
            Db::commit();
            return [true,'绑定成功5'];
        }catch (Exception $e){
            Db::rollback();
            return [false,$e->getMessage()];
        }

    }


}
